/*
* @Author: chenggh
* @Date: 2025-06-23
* @Link to Problem : https://oj.neu.edu.cn/courses/27/part/224/problem/1013
* 区间调度
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
typedef pair<int, int> PII;
class Solution{
public:
	int a, b;
	int id;
	Solution(int a, int b, int id):a(a), b(b), id(id){}
	Solution():a(0), b(0){}
	// 到达时间排序
	bool operator<(Solution b){
		return this->a < b.a;
	}
};
int map[200005] = {0};
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int n; cin >> n;
	vector<Solution> v;
	for(int i = 0, a, b; i < n; i++){
		cin >> a >> b;
		v.push_back(Solution(a, b, i));
	}
	sort(v.begin(), v.end());
	// 建小根堆管理房间的空闲情况
	set<PII> s;
	int cnt = 0;
	for(auto x: v){
		if(s.empty() || (*s.begin()).first >= x.a){
			s.insert(PII(x.b, ++cnt));
			map[x.id] = cnt;
			continue;
		}
		int id = (*s.begin()).second;
		s.erase(s.begin());
		s.insert(PII(x.b, id));
		map[x.id] = id;
	}
	cout << cnt << endl;

	for(int i = 0; i < n; i++) cout << map[i] << " ";
	cout << endl;
	return 0;
}